from __future__ import absolute_import
import uuid
from os.path import exists
from forest.api import Command, get_tool

from os import stat

class Dbus(Command):
    ''' Initializes dbus by creating a /etc/machine-id file.

        Example::

            dbus: False

        .. describe:: dbus

            Boolean specifying whether to initialize /etc/machine-id file. Defaults to True.
    '''
    __register__ = 'commands.setup.Dbus'

    def run(self, config):
        self.config = config.get('dbus', True)

        if not self.config:
            return

        machine_id_path = config.join_install_root('etc', 'machine-id')

        if exists(machine_id_path) and stat(machine_id_path).st_size != 0:
            return

        get_tool('files.write')(machine_id_path,  uuid.uuid4().hex)

